home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
UTIL
/
Acme Filters 2.3.2 folder.sit
/
Acme Filters 2.3.2 folder
/
Acme Filters 2.3.2
/
Acme Filters オ
/
Sources
/
Touch.c
< prev
next >
Wrap
Text File
|
1996-02-03
|
8KB
|
299 lines
/************ I N C L U D E F I L E S *******************************************************/
#include <stdio.h>
#include <string.h>
#include "Acme Filters.h"
#include "Touch.h"
/**********************************************************************************************
* Function Name: Touch
*
* Description: Set a file's creation and modification dates/times.
*
* Parameters: iFile - file spec of a file.
*
* Return Value: None.
**********************************************************************************************/
void Touch( FSSpec *iFile, unsigned long created, unsigned long modified )
{
HFileParam iInfo;
unsigned long secs;
DateTimeRec dt;
gCancelReq |= Cancel();
ScrollIcon();
// get the file's information
iInfo.ioNamePtr = iFile->name;
iInfo.ioVRefNum = iFile->vRefNum;
iInfo.ioDirID = iFile->parID;
iInfo.ioFDirIndex = 0;
PBHGetFInfo( (HParmBlkPtr)&iInfo, false );
// get the number of seconds since 1-Jan-1904
GetDateTime( &secs );
Secs2Date( secs, &dt );
dt.second = 0;
Date2Secs( &dt, &secs );
// set the file's information
iInfo.ioNamePtr = iFile->name;
iInfo.ioVRefNum = iFile->vRefNum;
iInfo.ioDirID = iFile->parID;
iInfo.ioFDirIndex = 0;
iInfo.ioFlMdDat = (long)secs;
if ( created && modified )
{
iInfo.ioFlCrDat = (long)created;
iInfo.ioFlMdDat = (long)modified;
}
PBHSetFInfo( (HParmBlkPtr)&iInfo, false );
PBFlushFile( (ParmBlkPtr)&iInfo, false );
TickleParentDir( iFile );
}
/**********************************************************************************************
* Function Name: PickDateTime
*
* Description: View or specify a file's creation and modification dates/times.
*
* Parameters: iFile - file spec of a file.
* created - the file's creation date.
* modified - the file's modification date.
*
* Return Value: noErr if ok, error code otherwise.
**********************************************************************************************/
OSErr PickDateTime( FSSpec *iFile, unsigned long *created, unsigned long *modified )
{
OSErr err = noErr;
DialogPtr picker;
WindowPtr saveMe;
Boolean done = false;
short item;
ModalFilterUPP filter = NewModalFilterProc( PickDateTimeDlgFltr );
HFileParam iInfo;
DateTimeRec dt;
short iType;
Handle iHandle;
Rect iRect;
Str255 str;
if ( picker = GetNewDialog( kDateTimeDlog, nil, (WindowPtr)-1L ) )
{
// stuff the creation and modification dates into the two TE fields
// get the file's information
iInfo.ioNamePtr = iFile->name;
iInfo.ioVRefNum = iFile->vRefNum;
iInfo.ioDirID = iFile->parID;
iInfo.ioFDirIndex = 0;
PBHGetFInfo( (HParmBlkPtr)&iInfo, false );
DateToString( iInfo.ioFlCrDat, str );
GetDItem( picker, kCreated, &iType, &iHandle, &iRect );
SetIText( iHandle, str );
DateToString( iInfo.ioFlMdDat, str );
GetDItem( picker, kModified, &iType, &iHandle, &iRect );
SetIText( iHandle, str );
SelIText( picker, kCreated, 0, kMaxDateTime );
GetPort( &saveMe );
SetPort( picker );
ShowWindow( picker );
while ( !done )
{
ModalDialog( filter, &item );
switch( item )
{
case -1: // -1 + 1 = 0, ok
case -2: // -2 + 1 = -1, cancel
err = item + 1;
done = true;
break;
case kCreated:
case kModified:
GetDItem( picker, item, &iType, &iHandle, &iRect );
GetIText( iHandle, str );
if ( str[0] > kMaxDateTime )
{
str[0] = kMaxDateTime;
SetIText( iHandle, str );
SysBeep( 1 );
}
break;
}
}
if ( !err )
{
// retrieve the creation and modification dates from the two TE fields
for ( item = kCreated; item <= kModified; item++ )
{
GetDItem( picker, item, &iType, &iHandle, &iRect );
memset( (char *)str, kNil, 256 );
GetIText( iHandle, str );
sscanf( (char *)&str[1], kDateTimeFormat,
&dt.month, &dt.day, &dt.year, &dt.hour, &dt.minute, &dt.second );
if ( item == kCreated ) Date2Secs( &dt, created );
if ( item == kModified ) Date2Secs( &dt, modified );
}
}
SetPort( saveMe );
DisposDialog( picker );
}
return ( err );
}
/**********************************************************************************************
* Function Name: PickDateTimeDlgFltr
*
* Description: Handle mouse and keystrokes.
*
* Parameters: dialog - the dialog being filtered.
* event - the event record being processed.
* item - the item in which the event occured.
*
* Return Value: true - ModalDialog returns immediately and without handling the event.
* false - ModalDialog handles the event normally.
**********************************************************************************************/
pascal Boolean PickDateTimeDlgFltr( DialogPtr dialog, EventRecord *event, short *item )
{
char c;
Boolean result = false;
*item = 0;
if ( (event->what == keyDown) || (event->what == autoKey) )
{
c = event->message & charCodeMask;
if ( (c == kEnter) || (c == kReturn) )
{
if ( DateTimeOK( dialog, kCreated ) && DateTimeOK( dialog, kModified ) )
*item = -1;
result = true;
}
if ( (c == kEscape) || ((event->modifiers & cmdKey) && (c == kPeriod)) )
{
*item = -2;
result = true;
}
}
return ( result );
}
/**********************************************************************************************
* Function Name: DateTimeOK
*
* Description: Validate the numbers in the date and time.
*
* Parameters: dialog - a dialog.
* item - the text edit item to check.
*
* Return Value: true if ok, false otherwise.
**********************************************************************************************/
Boolean DateTimeOK( DialogPtr dialog, short item )
{
short iType;
Handle iHandle;
Rect iRect;
Str255 str;
DateTimeRec dt;
Boolean result = true;
GetDItem( dialog, item, &iType, &iHandle, &iRect );
memset( (char *)str, kNil, 256 );
GetIText( iHandle, str );
if ( sscanf( (char *)&str[1], kDateTimeFormat,
&dt.month, &dt.day, &dt.year, &dt.hour, &dt.minute, &dt.second ) != 6 ) result = false;
if ( (dt.month < 1) || (dt.month > 12) ) result = false;
if ( (dt.day < 1) || (dt.day > DaysInMonth( dt )) ) result = false;
if ( (dt.year < 1904) || (dt.year > 2040) ) result = false;
if ( (dt.hour < 0) || (dt.hour > 23) ) result = false;
if ( (dt.minute < 0) || (dt.minute > 59) ) result = false;
if ( (dt.second < 0) || (dt.second > 59) ) result = false;
return ( result );
}
/**********************************************************************************************
* Function Name: DateToString
*
* Description: Create a MM/DD/YYYY HH:MM:SS formatted string.
*
* Parameters: secs - the date in seconds since 01-Jan-1904.
* date - the date after formatting.
*
* Return Value: None.
**********************************************************************************************/
void DateToString( unsigned long secs, Str255 date )
{
DateTimeRec dt;
Str255 temp;
Secs2Date( secs, &dt );
date[0] = 0;
// format date
if ( dt.month < 10) pstrcat( date, kLeadingZero );
NumToString( dt.month, temp );
pstrcat( date, temp );
pstrcat( date, kSlash );
if ( dt.day < 10) pstrcat( date, kLeadingZero );
NumToString( dt.day, temp );
pstrcat( date, temp );
pstrcat( date, kSlash );
NumToString( dt.year, temp );
pstrcat( date, temp );
pstrcat( date, kTwoSpaces );
// format time
if ( dt.hour < 10) pstrcat( date, kLeadingZero );
NumToString( dt.hour, temp );
pstrcat( date, temp );
pstrcat( date, kColon );
if ( dt.minute < 10) pstrcat( date, kLeadingZero );
NumToString( dt.minute, temp );
pstrcat( date, temp );
pstrcat( date, kColon );
if ( dt.second < 10) pstrcat( date, kLeadingZero );
NumToString( dt.second, temp );
pstrcat( date, temp );
}
short DaysInMonth( DateTimeRec dt )
{
static short dpm[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return ( (LeapYear( dt ) && (dt.month == 2)) ? 29 : dpm[dt.month-1] );
}
Boolean LeapYear( DateTimeRec dt )
{
return ( (!(dt.year % 4) && (dt.year % 100)) || !(dt.year % 400) ? true : false );
}